﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.ComponentModel;
using System.Runtime.Remoting.Messaging;

namespace SharedFleetAccess
{
    public  static class FleetAccess
    {
        private static XMLReader reader;
        private static Boolean resultOfReserve;
        delegate Boolean VehicleReserver (String id);
      
        public static void RunFleetAccess()
        {
             reader = new XMLReader();

        }
        public static List<String> getVehicleList()
        {
            List<String> result = new List<string>();
            foreach (KeyValuePair<String,_Vehicle> entry in reader.Info){
                result.Add(entry.Key);
            }
            return result;
        }
        public static List<String> getCarList()
        {
            List<String> result = new List<string>();
            foreach (KeyValuePair<String, _Vehicle> entry in reader.Info)
            {
                if (entry.Value.GetType().Equals(typeof(_Car)))
                {
                    result.Add(entry.Key);
                }
            }
            return result;
        }
        public static List<String> getTruckList()
        {
            List<String> result = new List<string>();
            foreach (KeyValuePair<String, _Vehicle> entry in reader.Info)
            {
                if (entry.Value.GetType().Equals(typeof(_Truck)))
                {
                    result.Add(entry.Key);
                }
            }
            return result;
        }
        public static Dictionary<String, String> getAttributes(String VehicleId)
        {
            // buscar como seria para acceder a los atributos propios de las clases herederas coche y camion
           
            Dictionary<String, String> dic = new Dictionary<string, string>();
            _Vehicle valueReturned;
            reader.Info.TryGetValue(VehicleId, out valueReturned);
            if (valueReturned != null)
            {
                dic.Add("Reg_Number", valueReturned.Reg_number);
                 dic.Add("Price", valueReturned.Price);
                 dic.Add("Colour", valueReturned.Colour);
                 dic.Add("State", valueReturned.State);
                 dic.Add("Fuel", valueReturned.Fuel);

                if (valueReturned.GetType().Equals(typeof(_Car)))
                {
                    _Car c = ((_Car)valueReturned);
                    dic.Add("Type_car", c.Type_car);
                     dic.Add("Doors_number", c.Doors_number);
                }
                else if (valueReturned.GetType().Equals(typeof(_Truck)))
                {
                    _Truck c = ((_Truck)valueReturned);
                    dic.Add("High", c.High);
                     dic.Add("Large", c.Large);
                     dic.Add("Width", c.Width);
                     dic.Add("Tonnage", c.Tonnage);
                     dic.Add("Num_Wheels", c.Num_Wheels);
                }
            }
            return dic;
        }
        public static Boolean reserveVehicle(String vehicleId)
        {
            VehicleReserver reserver = new VehicleReserver(reader.reserve);
            reserver.BeginInvoke(vehicleId, new AsyncCallback(AfterReserve), null);

            return resultOfReserve;
        }
        private static void AfterReserve(IAsyncResult result)
        {
            AsyncResult async = (AsyncResult)result;
            VehicleReserver reserver = (VehicleReserver)async.AsyncDelegate;
            resultOfReserve = reserver.EndInvoke(result);
        }
      
    }
}
